/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Matakuliah;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import rploo_ciayoo.kontrol.KoneksiDatabase;


public class MakulKontrol {

    private Connection conn;

    public MakulKontrol(Connection koneksi) {
        this.conn = koneksi;
    }

    public static MakulKontrol getKoneksi() throws SQLException {
        KoneksiDatabase konek = new KoneksiDatabase();
        MakulKontrol kon = new MakulKontrol(konek.getDBConnection());
        return kon;
    }

      public void tambahMakul(Matakuliah data) throws SQLException {
        PreparedStatement statement = null;
        try {
            conn.setAutoCommit(false);
            String sql = "insert into matakuliah "
                    + "(id_matakuliah,nama_matakuliah)"
                    + "values (?,?)";
            statement = conn.prepareStatement(sql);
            statement.setString(1, data.getIdmatakuliah());
            statement.setString(2, data.getNamaMatakuliah());
            statement.executeUpdate();
            conn.commit();
        } catch (SQLException exception) {
            conn.rollback();
            throw exception;
        } finally {
            try {
                conn.setAutoCommit(true);
                if (statement != null) {
                    statement.close();
                }
            } catch (SQLException exception) {
                throw exception;
            }
        }
    }

      public List<Matakuliah> lihatDataMakul() throws SQLException {
        PreparedStatement statement = null;
        ResultSet result = null;
        try {
            conn.setAutoCommit(false);

            statement = conn.prepareStatement("Select id_matakuliah, nama_matakuliah  "
                    + "from matakuliah "
                    + "order by id_matakuliah");

            result = statement.executeQuery();
            List<Matakuliah> data = new ArrayList<Matakuliah>();
            while (result.next()) {
                Matakuliah makul = new Matakuliah();
                makul.setIdMatakuliah(result.getString(1));
                makul.setNamaMatakuliah(result.getString(2));

                data.add(makul);
            }
            conn.commit();
            return data;
        } catch (SQLException exception) {
            conn.rollback();
            throw exception;
        } finally {
            try {
                conn.setAutoCommit(true);
                if (statement != null) {
                    statement.close();
                }
            } catch (SQLException exception) {
                throw exception;
            }
        }
    }
      public List<Matakuliah> getIdDosen() throws SQLException {
        PreparedStatement statement = null;
        ResultSet result = null;
        try {
            conn.setAutoCommit(false);

            statement = conn.prepareStatement("Select id_matakuliah"
                    + "from matakuliah "
                    + "order by id_matakuliah");

            result = statement.executeQuery();
            List<Matakuliah> data = new ArrayList<Matakuliah>();
            while (result.next()) {
                Matakuliah makul = new Matakuliah();
                makul.setIdMatakuliah(result.getString(1));
//                makul.setNamaMatakuliah(result.getString(2));

                data.add(makul);
            }
            conn.commit();
            return data;
        } catch (SQLException exception) {
            conn.rollback();
            throw exception;
        } finally {
            try {
                conn.setAutoCommit(true);
                if (statement != null) {
                    statement.close();
                }
            } catch (SQLException exception) {
                throw exception;
            }
        }
    }

       public void updateMakul(Matakuliah makul) throws SQLException {
        PreparedStatement statement = null;
        conn.setAutoCommit(false);
//        Barang kodeBarang = stock.getKodebarang();
//        String kode = kodeBarang.getKodeBarang();
        String sql = "update matakuliah set nama_matakuliah = ? " 
                + "where id_matakuliah = ? ";
        statement = conn.prepareStatement(sql);
        statement.setString(1, makul.getNamaMatakuliah());
        statement.setString(2, makul.getIdmatakuliah());
    
        statement.executeUpdate();
        conn.commit();
    }

        public void hapusPrepared(Matakuliah dataMakul) throws SQLException {
        PreparedStatement statement = null;
        try {
            conn.setAutoCommit(false);
            String sql = "delete from matakuliah where id_matakuliah = ? and nama_matakuliah = ?";
            statement = conn.prepareStatement(sql);
            statement.setString(1, dataMakul.getIdmatakuliah());
            statement.setString(2, dataMakul.getNamaMatakuliah());
            statement.executeUpdate();
            conn.commit();
        } catch (SQLException ex) {
            conn.rollback();
            throw ex;
        } finally {
            try {
                conn.setAutoCommit(true);
                if (statement != null) {
                    statement.close();
                }
            } catch (SQLException ex) {
                throw ex;
            }
        }
    }
}
